import Mock from 'mockjs'
import moment from 'moment'
const Random = Mock.Random
let activityList = []
let now = moment().unix()
let formatStr = 'YYYY-MM-DD HH:mm:SS'
let nowFormat = moment().format(formatStr)
for (let i = 0; i < 100; i++) {
    let startTime = moment().add(Random.integer(1, 100), 'days').unix()
    let endTime = Random.integer(startTime + 3600, startTime + 3600 * 24 * 10)
    let obj = Mock.mock({
        id: '@increment',
        activityName: '@cname',
        'status|1-4': 1,
        startTime: startTime,
        endTime: endTime,
        'type|1-2': 1,
        createUser: 'admin',
        createTime: now,
        'activityType|1-2': 1,
        createTimeString: nowFormat,
        startTimeString: moment.unix(startTime).format(formatStr),
        endTimeString: moment.unix(endTime).format(formatStr)
    })
    activityList.push(obj)
}

let activityItems = []
for (let key in activityList) {
    let activity = activityList[key]
    let itemcount = Random.integer(0, 20)
    for (let i = 0; i < itemcount; i++) {
        let obj = Mock.mock({
            id: '@increment',
            activityId: activity.id,
            productId: '@increment',
            productName: '@cname',
            salePrice: Random.float(0.1, 100.1),
            'isToUse|0-1': 0,
            usePrice: Random.float(0.1, 100.1),
            'stockLimit|1-100': 1,
            'status|0-1': 1
        })
        activityItems.push(obj)
    }
}

let activityOrders = []
for (let key in activityItems) {
    let item = activityItems[key]
    let itemcount = Random.integer(0, 20)
    let startTime = moment().add(Random.integer(1, 100), 'days').unix()
    for (let i = 0; i < itemcount; i++) {
        let obj = Mock.mock({
            id: '@increment',
            activityId: item.activityId,
            memberId: Random.integer(774180),
            orderSn: Random.integer(10000000),
            productId: item.productId,
            'quantity|1-3': 1,
            'status|1-3': 1,
            payTime: startTime,
            payTimeString: moment.unix(startTime).format(formatStr)
        })
        activityOrders.push(obj)
    }
}

export default [
    {
        url: '/getActivityList.json',
        method: 'get',
        statusCode: 200,
        response: (query) => {
            let deslist = activityList
                .filter((v) => (query.timeStart ? moment(query.timeStart).unix() <= v.startTime : true))
                .filter((v) => (query.timeEnd ? moment(query.timeEnd).unix() >= v.startTime : true))
                .filter((v) => (query.activityName ? query.activityName == v.activityName : true))
                .filter((v) => (query.status ? query.status == v.status : true))
                .filter((v) => (query.createUser ? query.createUser == v.createUser : true))
            return {
                code: 200,
                msg: 'OK',
                data: {
                    totalItemNum: deslist.length,
                    page: 1,
                    pageSize: query.size,
                    items: deslist.slice(query.start, parseInt(query.start) + parseInt(query.size))
                }
            }
        }
    },
    {
        url: '/getActivityItemList.json',
        method: 'get',
        statusCode: 200,
        response: (query) => {
            let deslist = activityItems
                .filter((v) => (query.activityId ? query.activityId == v.activityId : true))
                .filter((v) => (query.productId ? query.productId == v.productId : true))
            return {
                code: 200,
                msg: 'OK',
                data: {
                    totalItemNum: deslist.length,
                    page: 1,
                    pageSize: query.size,
                    items: deslist.slice(query.start, parseInt(query.start) + parseInt(query.size))
                }
            }
        }
    },
    {
        url: '/getActivityOrders.json',
        method: 'get',
        statusCode: 200,
        response: (query) => {
            let deslist = activityOrders.filter((v) => (query.productId ? query.productId == v.productId : true))
            return {
                code: 200,
                msg: 'OK',
                data: deslist
            }
        }
    },
    {
        url: '/updateActivityStockLimit.json',
        method: 'post',
        statusCode: 200,
        response: (query) => {
            activityItems.find((v) => v.activityId == query.activityId && v.productId == query.productId).stockLimit =
                query.stockLimit
            return {
                code: 200,
                msg: 'OK',
                data: {}
            }
        }
    },
    {
        url: '/doInvalidActivityProduct.json',
        method: 'post',
        statusCode: 200,
        response: (query) => {
            activityItems.find((v) => v.activityId == query.activityId && v.productId == query.productId).status = 0
            return {
                code: 200,
                msg: 'OK',
                data: {}
            }
        }
    },
    {
        url: '/addActivity.json',
        method: 'post',
        statusCode: 200,
        response: (query) => {
            let now = moment().unix()
            let nowFormat = moment().format(formatStr)
            activityList.unshift({
                id: '@increment',
                activityName: query.activityName,
                status: 1,
                startTime: moment(query.startTime).unix(),
                endTime: moment(query.endTime).unix(),
                type: query.type,
                createUser: 'admin',
                createTime: now,
                activityType: query.type,
                createTimeString: nowFormat,
                startTimeString: query.startTime,
                endTimeString: query.endTime
            })
            return {
                code: 200,
                msg: 'OK',
                data: {}
            }
        }
    },
    {
        url: '/isActivityNameExist.json',
        method: 'get',
        statusCode: 200,
        response: (query) => {
            return {
                code: 200,
                msg: 'OK',
                data: activityList.filter((v) => v.activityName == query.activityName && v.id != query.activityId)
                    .length
            }
        }
    },
    {
        url: '/doCancelActivity.json',
        method: 'post',
        statusCode: 200,
        response: (query) => {
            activityList.find((v) => v.id == query.activityId).status = 4
            return {
                code: 200,
                msg: 'OK',
                data: {}
            }
        }
    },
    {
        url: '/updateActivity.json',
        method: 'post',
        statusCode: 200,
        response: (query) => {
            let activity = activityList.find((v) => v.id == query.activityId)
            activity.activityName = query.activityName
            activity.type = query.type
            activity.startTime = moment(query.startTime).unix()
            activity.endTime = moment(query.endTime).unix()
            activity.startTimeString = query.startTime
            activity.endTimeString = query.endTime
            return {
                code: 200,
                msg: 'OK',
                data: {}
            }
        }
    }
]
